<?

/********************************************
* notifications.php
*
* This page displays a form to insert or delete notifications.
*
********************************************/

//include files
include "../config.php";
include "../../lib/db.php";
include "../../lib/tbatv_lib.php";

$title = "Notification Generator";
$relpath = "../";

include "../header.php";

//retrieve any form data:
$address = sanatizeGet($_REQUEST["emailaddress"]);
$teamnumber = sanatizeGet($_REQUEST["teamnumber"]);
//$eventid = sanatizeGet($_REQUEST["eventid"]);
$eventid = -1;
$iagree = sanatizeGet($_REQUEST["iagree"]);
$debug = sanatizeGet($_REQUEST['debug']);

function formError() {
	//I don't know why I have to do this again, but I do. Otherwise they don't echo out properly. Are they not global?
	
	//retrieve any form data:
	$address = sanatizeGet($_REQUEST["emailaddress"]);
	$teamnumber = sanatizeGet($_REQUEST["teamnumber"]);
	//$eventid = sanatizeGet($_REQUEST["eventid"]);
	$eventid = -1;
	$iagree = sanatizeGet($_REQUEST["iagree"]);
	$debug = sanatizeGet($_REQUEST['debug']);

	include "notificationform.php"; // Show the form again!
	include "../footer.php";
	exit(); // if the error checking has failed, we'll exit the script!
}

?>
<h1>Notification Subscription</h1>
<p>To unsubscribe, <a href="notification_delete.php">click here</a>.</p>
<?

if($_REQUEST["submit"] == "submit") {

	if((!$address) || (!$teamnumber) || (!$eventid) || (!$iagree) || (!is_numeric($teamnumber))) {
		echo '<div class="formItemWarning">You did not submit the following required information! <br />';
		if(!$address){
			echo "E-mail address is a required field. Please enter it below.<br />";
		}
		if(!$teammnumber){
			echo "Team number is a required field. Please enter it below.<br />";
		}
		if(!is_numeric($teamnumber)){
			echo "Team number must be a number. Please correct it below.<br />";
		}
		if(!$eventid){
			echo "Event is a required field. Please select it below.<br />";
		}
		if ($iagree != 1) {
			echo "Sorry, but you can not use TBA Notifications if you do not accept the checkboxes below.<br />";
		}
	   	echo '</div>';
		
		formError();
	}

	$avoid_dupes_filter = "SELECT address FROM sms_notifications WHERE address = '$address' AND team = '$teamnumber' AND event = '$eventid' AND verified = '1'";
	$avoid_dupes_query = mysql_query($avoid_dupes_filter);
	if (mysql_num_rows($avoid_dupes_query) > 0) {
		echo "There is already a notification for <strong>" . $address . "</strong> for team <strong>" . $teamnumber ."</strong>!";
	} else {
		$verification_code = rand(0,10000);
		mysql_query("INSERT INTO sms_notifications (address, team, event, verificationcode) VALUES ('$address', '$teamnumber', '$eventid', '$verification_code')");
		
		$id = mysql_insert_id();
		
		//echo "INSERT INTO sms_notifications (address, team, event, verificationcode) VALUES ('$address', '$teamnumber', '$eventid', '$verification_code')";
		
		$subject="TBA";
		$message="Your verification code is $verification_code";
		mail($address, $subject, $message,
			        "From: The Blue Alliance<texter@thebluealliance.net>\nX-Mailer: PHP/" . phpversion());
	}

?>
<h2>Notification Established!</h2>

<p>You have requested to add a notification for team <? echo $teamnumber ?> for the address <? echo $address ?>. A verification message has been sent to the address provided. Please enter it in the field below to confirm your subscription.</p>

<?

include "notification_verificationform.php";

} else { //This ends the "Submit" if. Now we include the "Not submitted" stuff.

?>
<p>The Blue Alliance can provide text message or e-mail updates about how a team is doing at a competition. Messages will look something like "177, 999, 157 lost to 173, 237, 237 [46 to 72] in Qualifications 74 at Connecticut Regional". To subscribe to these notifications, you may use the form on this page.</p>
<?

	include "notificationform.php";
}
include "../footer.php";
?>